package com.fcscs.cservice.client.main.reservation;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.AdapterField;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.Validator;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreConstants.ValidationListener;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.BaseFormDialog;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.IEFriendlyDateField;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.ExtTimeField;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.controller.ReservationServiceController;
import com.fcscs.cservice.client.common.model.reservation.CommonData;
import com.fcscs.cservice.client.common.model.reservation.CountryViewModelData;
import com.fcscs.cservice.client.common.model.reservation.GenderEnum;
import com.fcscs.cservice.client.common.model.reservation.GenderGWTEnum;
import com.fcscs.cservice.client.common.model.reservation.GenderGWTEnumModelData;
import com.fcscs.cservice.client.common.model.reservation.NationalityModelData;
import com.fcscs.cservice.client.common.model.reservation.NumberOfGuestData;
import com.fcscs.cservice.client.common.model.reservation.PaymentMethodModelData;
import com.fcscs.cservice.client.common.model.reservation.ReservationViewModelData;
import com.fcscs.cservice.client.common.model.reservation.StateViewModelData;
import com.fcscs.cservice.client.common.model.reservation.TitleModelData;
import com.fcscs.cservice.client.common.model.reservation.YesNoEnum;
import com.fcscs.cservice.client.common.model.reservation.YesNoGWTEnum;
import com.fcscs.cservice.client.common.model.reservation.YesNoGWTEnumModelData;
import com.fcscs.cservice.common.CServiceCommonEnums.EditableCategoryEnum;
import com.fcscs.cservice.common.CServiceCommonEnums.EditableFieldEnum;
import com.fcscs.cservice.domain.model.CountryView;
import com.fcscs.cservice.domain.model.EditabilityConf;
import com.fcscs.cservice.domain.model.RCView;
import com.fcscs.cservice.domain.model.ReservationConfView;
import com.fcscs.cservice.domain.model.ReservationConfView.Nationality;
import com.fcscs.cservice.domain.model.ReservationConfView.PaymentMethod;
import com.fcscs.cservice.domain.model.ReservationConfView.Title;
import com.fcscs.cservice.domain.model.ReservationView;
import com.fcscs.cservice.domain.model.StateView;
import com.fcscs.cservice.common.CServiceReportConstants;
import com.fcscs.share.common.ReportConstants;
import com.fcscs.share.gwt.common.HttpHelper;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.fcscs.share.gwt.common.ShareGWTEnums;
import com.fcscs.share.gwt.common.ShareGWTEnums.ExportType;
import com.fcscs.share.gwt.ui.ExportTypeDialog;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Frame;

public class RegistrationCardDialog extends BaseFormDialog<RCView> implements
		FormSubmitListener<RCView> {
	private MyFormPanel formPanel;
	private FormSubmitListener<RCView> listener;
	private static RegistrationCardDialog INSTANCE;
	private ReservationView rv;
	private RCView currentRC;
	private RoomRateDetailDialog rateDetailDialog;
	private ReservationConfView reservationConf;
	private List<CountryView> countryList;
	private boolean loadStateOver;
	private ReservationViewModelData rvModel;
	private boolean isProfile;
	final String emailReg = "[A-Z0-9._%+-]+@[A-Z0-9-]+\\.[A-Z]{2,4}";
	
	protected TextField<String> guestNameField;
	protected EasyClearCombobox<TitleModelData> titleCombo;
	protected EasyClearCombobox<GenderGWTEnumModelData> genderCombo;
	protected EasyClearCombobox<NationalityModelData> nationalityCombo;
	protected TextField<String> emailField;
	protected TextField<String> mobilField;
	private Frame reportFrame;
	protected FormButton exportBtn;
	private long rcId;
	private List<EditableCategoryEnum> categories;
	
	public static RegistrationCardDialog getInstance(FormSubmitListener<RCView> listener, ReservationViewModelData rvModel,
			ReservationConfView reservationConf, List<CountryView> countryList, boolean isProfile){
		if(null == INSTANCE){
			INSTANCE = new RegistrationCardDialog();
		}
		INSTANCE.listener = listener;
		INSTANCE.isProfile = isProfile;
		INSTANCE.rvModel = rvModel;
		INSTANCE.rv = rvModel.getEntity();
		INSTANCE.reservationConf = reservationConf;
		INSTANCE.countryList = countryList;
		INSTANCE.setHeading(CServiceConstants.CSERVICE_CONSTANTS.registrationCard());
		return INSTANCE;
	}
	
	private RegistrationCardDialog(){
		formPanel = new MyFormPanel(this);
		formPanel.setId("RegistrationCardDialog_formPanel");
		add(formPanel);
		setSize(700, 660);
	}
	
	private class MyFormPanel extends FcsFormPanel<RCView>{
		private LayoutContainer lc1;
		private LayoutContainer lc2;
		private LayoutContainer lc3;
		private LayoutContainer lc4;
		private LayoutContainer lc5;
		
		private Label confirmationNoLabel;
		private AdapterField confirmationNoAF;
		
		private Label spgNoLabel;
		private AdapterField spgNoAF;
		
//		private Label arrivalDateTimeLabel;
//		private AdapterField arrivalDateTimeAF;
		
		private Label arrivalDateAndTimeLabel;
		private IEFriendlyDateField arrivalDateField;
		private ExtTimeField arrivalTimeField;
		
//		private Label departualDateLabel;
//		private AdapterField departualDateAF;
		
		private IEFriendlyDateField departureDateField;
		
		private Label gsaLabel;
		private AdapterField gsaAF;
		
//		private TextField<String> gsaField;
		
		private Label roomRateLabel;
		private Label roomRateValueLabel;
		
//		private Label guestNameLabel;
//		private AdapterField guestNameAF;
		
//		private Label titleLabel;
//		private AdapterField titleAF;
		
		private ListStore<TitleModelData> titleListStore;
		
//		private Label genderLabel;
//		private AdapterField genderAF;
		
		private ListStore<GenderGWTEnumModelData> genderListStore;
		
//		private Label noOfGuestLabel;
//		private AdapterField noOfGuestAF;
		
		private EasyClearCombobox<NumberOfGuestData> noOfGuestCombo;
		private ListStore<NumberOfGuestData> noOfGuestStore;

		private Label roomSuiteLabel;
		private AdapterField roomSuiteAF;
		
		private Label roomTypeLabel;
		private AdapterField roomTypeAF;
		
//		private Label shareReservationLabel;
//		private AdapterField shareReservationAF;
		
		private EasyClearCombobox<YesNoGWTEnumModelData> shareReservationCombo;
		private ListStore<YesNoGWTEnumModelData> shareReservationListStore;
		
		private Label depositLabel;
		private AdapterField depositAF;
		
		private Label guestInitialLabel;
		private AdapterField guestInitialAF;
		
//		private Label nationalityLabel;
//		private AdapterField nationalityAF;
		
		private ListStore<NationalityModelData> nationalityListStore;
		
//		private Label eMailAddressLabel;
//		private AdapterField eMailAddressAF;
		
//		private Label mobileLabel;
//		private AdapterField mobileAF;
		
		private Radio homeRadio;
		private Radio businessRadio;
		
		private RadioGroup radioGroup;
		
//		private Label addressLabel;
//		private AdapterField addressAF;
		private TextField<String> addressField;
		
//		private Label cityLabel;
//		private AdapterField cityAF;
		private TextField<String> cityField;
		
//		private Label countryLabel;
//		private AdapterField countryAF;
		
		private EasyClearCombobox<CountryViewModelData> countryCombo;
		private ListStore<CountryViewModelData> countryListStore;
		
//		private Label phoneLabel;
//		private AdapterField phoneAF;
		private TextField<String> phoneField;
		
//		private Label stateLabel;
//		private AdapterField stateAF;
		
		private EasyClearCombobox<StateViewModelData> stateCombo;
		private ListStore<StateViewModelData> stateListStore;
		
//		private Label zipLabel;
//		private AdapterField zipAF;
		private TextField<String> zipField;
		
		private Label specialServicesLabel;
		private AdapterField specialServicesAF;
		
//		private Label remarksLabel;
//		private AdapterField remarksAF;
		private TextField<String> remarksField;
		
		private Label billingInstructionLabel;
		private AdapterField billingInstructionAF;
		
		private Label guaranteeLabel;
		private AdapterField guaranteeAF;
		
//		private Label expressCheckOutLabel;
//		private AdapterField expressCheckOutAF;
		
		private EasyClearCombobox<YesNoGWTEnumModelData> expressCheckOutCombo;
		private ListStore<YesNoGWTEnumModelData> expressCheckOutListStore;
		
//		private Label paymentCommentLabel;
//		private AdapterField paymentCommentAF;
		private TextField<String> paymentCommentField;
		
//		private Label paymentMethodLabel;
//		private AdapterField paymentMethodAF;
		
		private EasyClearCombobox<PaymentMethodModelData> paymentMethodCombo;
		private ListStore<PaymentMethodModelData> paymentMethodListStore;
		
		private Label dbCodeLabel;
		private AdapterField dbCodeAF;
		
		private TextArea ta;
		
		private FormButton saveButton;
		private FormButton cancelButton;
		
		private Validator emailValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String value) {
				if(value == null){
					return CoreConstants.CORE_CONSTANTS.requiredField();
				}else if(value.trim().isEmpty()){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
				}else if(value.trim().length()>100){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.email(), 100);
				}else if(!value.toUpperCase().matches(emailReg)){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.emailErro();
				}
				return null;
			}
		};
		
		private Validator mobileValidator = new Validator() {
			@Override
			public String validate(Field<?> field, final String value) {
				if(value == null){
					return CoreConstants.CORE_CONSTANTS.requiredField();
				}else if(value.trim().isEmpty()){
					return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
				}else if(value.trim().length()>50){
					return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(
							CoreConstants.CORE_CONSTANTS.mobile(), 50);
				}else if(!isValidMobile(value.trim())){
					return CServiceConstants.CSERVICE_CONSTANTS.enterNumber();
				}
				return null;
			}
		};
		
		protected MyFormPanel(FormSubmitListener<RCView> l) {
			super(l);
			setHeaderVisible(false);
			
			lc1 = new LayoutContainer();
			lc1.setId("RegistrationCardDialog_lc1");
			lc1.setLayout(new ColumnLayout());
			lc1.setBorders(true);
			
			LayoutContainer left1 = new LayoutContainer();
			left1.setId("RegistrationCardDialog_left1");
			FormLayout layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(180);
			left1.setLayout(layout);
			
			confirmationNoLabel = new Label();
			confirmationNoLabel.setId("RegistrationCardDialog_confirmationNoLabel");
			
			confirmationNoAF = new AdapterField(confirmationNoLabel);
			confirmationNoAF.setId("RegistrationCardDialog_confirmationNoAF");
			confirmationNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.confirmationNO());
			left1.add(confirmationNoAF);
			
			spgNoLabel = new Label();
			spgNoLabel.setId("RegistrationCardDialog_spgNoLabel");
			
			spgNoAF = new AdapterField(spgNoLabel);
			spgNoAF.setId("RegistrationCardDialog_spgNoAF");
			spgNoAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.spgNO());
			left1.add(spgNoAF);
			
//			arrivalDateTimeLabel = new Label();
//			arrivalDateTimeLabel.setId("RegistrationCardDialog_arrivalDateTimeLabel");
//			
//			arrivalDateTimeAF = new AdapterField(arrivalDateTimeLabel);
//			arrivalDateTimeAF.setId("RegistrationCardDialog_arrivalDateTimeAF");
//			arrivalDateTimeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.arrivalDateTime());
//			left1.add(arrivalDateTimeAF);
			
			arrivalDateAndTimeLabel = new Label("<html>" +"<font size=\"2\">" 
					+ CServiceConstants.CSERVICE_CONSTANTS.arrivalDateTime() + ":" 
					+ "<font>" +"</html>");
			arrivalDateAndTimeLabel.setId("RegistrationCardDialog_arrivalDateAndTimeLabel");
			
			arrivalDateField = new IEFriendlyDateField();
			arrivalDateField.setId("RegistrationCardDialog_arrivalDateField");
			arrivalDateField.setEmptyText(CoreConstants.CORE_CONSTANTS.date());
			arrivalDateField.setAllowBlank(false);
			arrivalDateField.setEditable(false);
			arrivalDateField.setMessageTarget("tooltip");
			arrivalDateField.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
			
			arrivalTimeField = new ExtTimeField();
			arrivalTimeField.setId("RegistrationCardDialog_arrivalTimeField");
			arrivalTimeField.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "20px");
			arrivalTimeField.addListener(Events.OnBlur, new Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					if(arrivalTimeField.getValue() != null && arrivalDateField.getValue() == null){
						arrivalDateField.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if((arrivalTimeField.getRawValue() != null)
							&& (!arrivalTimeField.getRawValue().trim().isEmpty())
							&& !isValidTime(arrivalTimeField.getRawValue().trim())){
						arrivalTimeField.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.validTime());
						return;
					}else{
						arrivalTimeField.clearInvalid();
						return;
					}
				}
			});
			
			LayoutContainer hp = new LayoutContainer();
			hp.setId("RegistrationCardDialog_hp");
			hp.setLayout(new ColumnLayout());
			
			hp.add(arrivalDateAndTimeLabel, new ColumnData(120));
			hp.add(arrivalDateField, new ColumnData(100));
			hp.add(arrivalTimeField, new ColumnData(100));
			
			left1.add(hp);
			
//			departualDateLabel = new Label();
//			departualDateLabel.setId("RegistrationCardDialog_departualDateLabel");
//			
//			departualDateAF = new AdapterField(departualDateLabel);
//			departualDateAF.setId("RegistrationCardDialog_departualDateAF");
//			departualDateAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.departureDate());
//			left1.add(departualDateAF);
			
			departureDateField = new IEFriendlyDateField();
			departureDateField.setId("RegistrationCardDialog_departureDateField");
			departureDateField.setEmptyText(CoreConstants.CORE_CONSTANTS.date());
			departureDateField.setAllowBlank(false);
			departureDateField.setEditable(false);
			departureDateField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.departureDate());
			left1.add(departureDateField);
			
			gsaLabel = new Label();
			gsaLabel.setId("RegistrationCardDialog_gsaLabel");
			
			gsaAF = new AdapterField(gsaLabel);
			gsaAF.setId("RegistrationCardDialog_gsaAF");
			gsaAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.gsa());
			left1.add(gsaAF);
			
//			gsaField = new TextField<String>();
//			gsaField.setId("RegistrationCardDialog_gsaField");
//			gsaField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.gsa());
//			left1.add(gsaField);
				
			LayoutContainer roomRateLc = new LayoutContainer();
			roomRateLc.setId("RegistrationCardDialog_roomRateLc");
			roomRateLc.setLayout(new ColumnLayout());
			
			roomRateLabel = new Label();
			roomRateLabel.setId("RegistrationCardDialog_roomRateLabel");
			roomRateLabel.setText("<html>" +"<font size=\"2\">" 
		               + CServiceConstants.CSERVICE_CONSTANTS.roomRate() + ":"
		               + "<font>" +"</html>");
			roomRateLc.add(roomRateLabel, new ColumnData(125));
			
			roomRateValueLabel = new Label();
			roomRateValueLabel.setId("RegistrationCardDialog_roomRateValueLabel");
			roomRateLc.add(roomRateValueLabel);
			left1.add(roomRateLc);
			
//			guestNameLabel = new Label();
//			guestNameLabel.setId("RegistrationCardDialog_guestNameLabel");
//			
//			guestNameAF = new AdapterField(guestNameLabel);
//			guestNameAF.setId("RegistrationCardDialog_guestNameAF");
//			guestNameAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
//			left1.add(guestNameAF);
			
			guestNameField = new TextField<String>();
			guestNameField.setId("RegistrationCardDialog_guestNameField");
			guestNameField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			guestNameField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.guestName());
			guestNameField.setAllowBlank(false);
			guestNameField.setValidator(new Validator(){
				@Override
				public String validate(Field<?> field, String value) {
					if(null == value){
						return CoreConstants.CORE_CONSTANTS.requiredField();
					}else if(value.trim().isEmpty()){
						return ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty();
					}else if(!value.trim().contains(",")){
						return CServiceConstants.CSERVICE_CONSTANTS.validName();
					}else if(!isValidName(guestNameField.getValue().trim())){
						return ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.firstname() 
								+ ShareGWTConstants.SHARE_GWT_CONSTANTS.or()
								+ CoreConstants.CORE_CONSTANTS.lastname(), 255);
					}
					return null;
				}
			});
			left1.add(guestNameField);
			
//			titleLabel = new Label();
//			titleLabel.setId("RegistrationCardDialog_titleLabel");
//			
//			titleAF = new AdapterField(titleLabel);
//			titleAF.setId("RegistrationCardDialog_titleAF");
//			titleAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.title());
//			left1.add(titleAF);
			
			titleListStore = new ListStore<TitleModelData>();
			titleCombo = new EasyClearCombobox<TitleModelData>();
			titleCombo.setId("RegistrationCardDialog_titleCombo");
			titleCombo.setStore(titleListStore);
			titleCombo.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.title());
			titleCombo.setDisplayField(CServiceConstants.TITLE);
			titleCombo.setTriggerAction(TriggerAction.ALL);
			titleCombo.setForceSelection(true);
			titleCombo.setEditable(false);
			left1.add(titleCombo);
			
//			genderLabel = new Label();
//			genderLabel.setId("RegistrationCardDialog_genderLabel");
//			
//			genderAF = new AdapterField(genderLabel);
//			genderAF.setId("RegistrationCardDialog_genderAF");
//			genderAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.gender());
//			left1.add(genderAF);
			
			genderListStore = new ListStore<GenderGWTEnumModelData>();
			for(GenderGWTEnum ge : GenderGWTEnum.values()){
				genderListStore.add(new GenderGWTEnumModelData(ge));
			}
			genderCombo = new EasyClearCombobox<GenderGWTEnumModelData>();
			genderCombo.setId("RegistrationCardDialog_genderCombo");
			genderCombo.setStore(genderListStore);
			genderCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.gender());
			genderCombo.setDisplayField(CoreConstants.FULLTNAME);
			genderCombo.setTriggerAction(TriggerAction.ALL);
			genderCombo.setForceSelection(true);
			genderCombo.setEditable(false);
			left1.add(genderCombo);
			
			LayoutContainer right1 = new LayoutContainer();
			right1.setId("RegistrationCardDialog_right1");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(160);
			right1.setLayout(layout);
			
			noOfGuestStore = new ListStore<NumberOfGuestData>();
			List<NumberOfGuestData> nums = CommonData.getNums();
			noOfGuestStore.add(nums);
			noOfGuestCombo = new EasyClearCombobox<NumberOfGuestData>();
			noOfGuestCombo.setId("GuestProfileDialog_noOfGuestCombo");
			noOfGuestCombo.setStore(noOfGuestStore);
			noOfGuestCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.noOfGuest());
			noOfGuestCombo.setDisplayField(CoreConstants.FULLTNAME);
			noOfGuestCombo.setTriggerAction(TriggerAction.ALL);
			noOfGuestCombo.setForceSelection(true);
			noOfGuestCombo.setEditable(false);
			right1.add(noOfGuestCombo);
			
			roomSuiteLabel = new Label();
			roomSuiteLabel.setId("RegistrationCardDialog_roomSuiteLabel");
			
			roomSuiteAF = new AdapterField(roomSuiteLabel);
			roomSuiteAF.setId("RegistrationCardDialog_roomSuiteAF");
			roomSuiteAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.roomSuite());
			right1.add(roomSuiteAF);
			
			roomTypeLabel = new Label();
			roomTypeLabel.setId("RegistrationCardDialog_roomTypeLabel");
			
			roomTypeAF = new AdapterField(roomTypeLabel);
			roomTypeAF.setId("RegistrationCardDialog_roomSuiteAF");
			roomTypeAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.roomType());
			right1.add(roomTypeAF);
			
//			shareReservationLabel = new Label();
//			shareReservationLabel.setId("RegistrationCardDialog_shareReservationLabel");
//			
//			shareReservationAF = new AdapterField(shareReservationLabel);
//			shareReservationAF.setId("RegistrationCardDialog_shareReservationAF");
//			shareReservationAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.shareReservation());
//			right1.add(shareReservationAF);
			
			shareReservationListStore = new ListStore<YesNoGWTEnumModelData>();
			for(YesNoGWTEnum yn : YesNoGWTEnum.values()){
				shareReservationListStore.add(new YesNoGWTEnumModelData(yn));
			}
			shareReservationCombo = new EasyClearCombobox<YesNoGWTEnumModelData>();
			shareReservationCombo.setId("RegistrationCardDialog_shareReservationCombo");
			shareReservationCombo.setStore(shareReservationListStore);
			shareReservationCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.shareReservation());
			shareReservationCombo.setDisplayField(CoreConstants.FULLTNAME);
			shareReservationCombo.setTriggerAction(TriggerAction.ALL);
			shareReservationCombo.setForceSelection(true);
			shareReservationCombo.setEditable(false);
			right1.add(shareReservationCombo);
			
			depositLabel = new Label();
			depositLabel.setId("RegistrationCardDialog_depositLabel");
			
			depositAF = new AdapterField(depositLabel);
			depositAF.setId("RegistrationCardDialog_depositAF");
			depositAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.deposit());
			right1.add(depositAF);
			
			guestInitialLabel = new Label();
			guestInitialLabel.setId("RegistrationCardDialog_guestInitialLabel");
			
			guestInitialAF = new AdapterField(guestInitialLabel);
			guestInitialAF.setId("RegistrationCardDialog_guestInitialAF");
			guestInitialAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.guestInitial());
			right1.add(guestInitialAF);
			
//			nationalityLabel = new Label();
//			nationalityLabel.setId("RegistrationCardDialog_nationalityLabel");
//			
//			nationalityAF = new AdapterField(nationalityLabel);
//			nationalityAF.setId("RegistrationCardDialog_nationalityAF");
//			nationalityAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.nationality());
//			right1.add(nationalityAF);
			
			nationalityListStore = new ListStore<NationalityModelData>();
			nationalityCombo = new EasyClearCombobox<NationalityModelData>();
			nationalityCombo.setId("RegistrationCardDialog_nationalityCombo");
			nationalityCombo.setStore(nationalityListStore);
			nationalityCombo.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.nationality());
			nationalityCombo.setDisplayField(CServiceConstants.NATIONALITY);
			nationalityCombo.setTriggerAction(TriggerAction.ALL);
			nationalityCombo.setForceSelection(true);
			nationalityCombo.setEditable(false);
			right1.add(nationalityCombo);
			
//			eMailAddressLabel = new Label();
//			eMailAddressLabel.setId("RegistrationCardDialog_eMailAddressLabel");
//			
//			eMailAddressAF = new AdapterField(eMailAddressLabel);
//			eMailAddressAF.setId("RegistrationCardDialog_eMailAddressAF");
//			eMailAddressAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.email());
//			right1.add(eMailAddressAF);
			
			emailField = new TextField<String>();
			emailField.setId("RegistrationCardDialog_emailField");
			emailField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			emailField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.email());
			emailField.setAllowBlank(false);
			emailField.setValidator(emailValidator);
			emailField.addListener(Events.OnBlur, new  Listener<BaseEvent>(){
				@Override
				public void handleEvent(BaseEvent be) {
					String value = emailField.getValue();
					if(value == null){
						emailField.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if(value.trim().isEmpty()){
						emailField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty());
						return;
					}else if(value.trim().length()>100){
						emailField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.email(), 100));
						return;
					}else if(!value.toUpperCase().matches(emailReg)){
						emailField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.emailErro());
						return;
					}else{
						emailField.clearInvalid();
						return;
					}
				}
			});
			right1.add(emailField);
			
//			mobileLabel = new Label();
//			mobileLabel.setId("RegistrationCardDialog_mobileLabel");
//			
//			mobileAF = new AdapterField(mobileLabel);
//			mobileAF.setId("RegistrationCardDialog_mobileAF");
//			mobileAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.mobile());
//			right1.add(mobileAF);
			
			mobilField = new TextField<String>();
			mobilField.setId("RegistrationCardDialog_mobilField");
			mobilField.setLabelSeparator(CoreConstants.REQURIED_FIELD_LABEL);
			mobilField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.mobile());
			mobilField.setValidator(mobileValidator);
			mobilField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = mobilField.getValue();
					if(value == null){
						mobilField.forceInvalid(CoreConstants.CORE_CONSTANTS.requiredField());
						return;
					}else if(value.trim().isEmpty()){
						mobilField.forceInvalid(ShareGWTConstants.SHARE_GWT_CONSTANTS.cannotEmpty());
						return;
					}else if(value.trim().length()>50){
						mobilField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.mobile(), 50));
						return;
					}else if(!isValidMobile(value.trim())){
						mobilField.forceInvalid(CServiceConstants.CSERVICE_CONSTANTS.enterNumber());
						return;
					}else{
						mobilField.clearInvalid();
						return;
					}
				}
			});
			right1.add(mobilField);
			
			lc1.add(left1, new ColumnData(.55));
			lc1.add(right1, new ColumnData(.45));
			
			add(lc1);
			
			lc4 = new LayoutContainer();
			lc4.setId("RegistrationCardDialog_lc4");
		    lc4.setLayout(new ColumnLayout());
		    lc4.setBorders(true);
		    
			homeRadio = new Radio();
			homeRadio.setId("RegistrationCardDialog_homeRadio");
			homeRadio.setBoxLabel("Home");

		    businessRadio = new Radio();
		    businessRadio.setId("RegistrationCardDialog_businessRadio");
		    businessRadio.setBoxLabel("Business");
			
			radioGroup = new RadioGroup();
			radioGroup.setId("RegistrationCardDialog_radioGroup");
//		    radioGroup.setFieldLabel("Favorite Color");
			radioGroup.setLabelSeparator("");
		    radioGroup.add(homeRadio);
		    radioGroup.add(businessRadio);
		    
		    lc4.add(radioGroup);
		    add(lc4);
		    
//		    addressLabel = new Label();
//		    addressLabel.setId("RegistrationCardDialog_addressLabel");
//			
//		    addressAF = new AdapterField(addressLabel);
//		    addressAF.setId("RegistrationCardDialog_addressAF");
//		    addressAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.address());
//			add(addressAF);
			
		    lc5 = new LayoutContainer();
		    lc5.setId("RegistrationCardDialog_lc5");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(180);
			lc5.setLayout(layout);
		    
			addressField = new TextField<String>();
			addressField.setId("RegistrationCardDialog_addressField");
			addressField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.address());
			addressField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = addressField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 255){
						addressField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(ShareGWTConstants.SHARE_GWT_CONSTANTS.address(), 255));
						return;
					}else{
						addressField.clearInvalid();
						return;
					}
				}
			});
			lc5.add(addressField, new ColumnData(.55));
			add(lc5);
		    
		    lc2 = new LayoutContainer();
		    lc2.setId("RegistrationCardDialog_lc2");
		    lc2.setLayout(new ColumnLayout());
		    lc2.setBorders(true);
		    
		    LayoutContainer left2 = new LayoutContainer();
		    left2.setId("RegistrationCardDialog_left2");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(180);
			left2.setLayout(layout);
			
//			cityLabel = new Label();
//			cityLabel.setId("RegistrationCardDialog_cityLabel");
//			
//			cityAF = new AdapterField(cityLabel);
//			cityAF.setId("RegistrationCardDialog_cityAF");
//			cityAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.city());
//			left2.add(cityAF);
			
			cityField = new TextField<String>();
			cityField.setId("RegistrationCardDialog_cityField");
			cityField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.city());
			cityField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = cityField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 50){
						cityField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(ShareGWTConstants.SHARE_GWT_CONSTANTS.city(), 50));
						return;
					}else{
						cityField.clearInvalid();
						return;
					}
				}
			});
			left2.add(cityField);
			
//			countryLabel = new Label();
//			countryLabel.setId("RegistrationCardDialog_countryLabel");
//			
//			countryAF = new AdapterField(countryLabel);
//			countryAF.setId("RegistrationCardDialog_countryAF");
//			countryAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.country());
//			left2.add(countryAF);
			
			countryListStore = new ListStore<CountryViewModelData>();
			countryCombo = new EasyClearCombobox<CountryViewModelData>();
			countryCombo.setId("RegistrationCardDialog_countryCombo");
			countryCombo.setStore(countryListStore);
			countryCombo.setFieldLabel(CoreConstants.CORE_CONSTANTS.country());
			countryCombo.setDisplayField(CServiceConstants.COUNTRY);
			countryCombo.setTriggerAction(TriggerAction.ALL);
			countryCombo.setForceSelection(true);
			countryCombo.setEditable(false);
			countryCombo.addSelectionChangedListener(new SelectionChangedListener<CountryViewModelData>(){
				@Override
				public void selectionChanged(
						SelectionChangedEvent<CountryViewModelData> se) {
					if(countryCombo.getValue() != null){
						loadStateOver = false;
					CServiceCommonServiceController.getINSTANCE().getStates(
							countryCombo.getValue().getEntity().getCountryId(), 
						new AsyncCallbackListener<List<StateView>>(){
							@Override
							public void onLogin(String username) {
								CServiceCommonServiceController.getINSTANCE().getStates(
										SessionContext.getInstance().getPropertyId(),this);
							}

							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(
										CoreConstants.CORE_MESSAGES.operation(
											CoreConstants.CORE_CONSTANTS.system(), 
											ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(), null);
							}

							@Override
							public void onSuccess(List<StateView> result) {
								loadStateOver = true;
								if(null != result){
									stateListStore.removeAll();
									stateCombo.clear();
									for(StateView sv : result){
										stateListStore.add(new StateViewModelData(sv));
									}
								}
							}
							
						});
					}
				}
			});
			left2.add(countryCombo);
			
//			phoneLabel = new Label();
//			phoneLabel.setId("RegistrationCardDialog_phoneLabel");
//			
//			phoneAF = new AdapterField(phoneLabel);
//			phoneAF.setId("RegistrationCardDialog_phoneAF");
//			phoneAF.setFieldLabel(CoreConstants.CORE_CONSTANTS.phone());
//			left2.add(phoneAF);
			
			phoneField = new TextField<String>();
			phoneField.setId("RegistrationCardDialog_phoneField");
			phoneField.setFieldLabel(CoreConstants.CORE_CONSTANTS.phone());
			phoneField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = phoneField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 50){
						phoneField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CoreConstants.CORE_CONSTANTS.phone(), 50));
						return;
					}else{
						phoneField.clearInvalid();
						return;
					}
				}
			});
			left2.add(phoneField);
			
			LayoutContainer right2 = new LayoutContainer();
			right2.setId("RegistrationCardDialog_right2");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(160);
			right2.setLayout(layout);
			
//			stateLabel = new Label();
//			stateLabel.setId("RegistrationCardDialog_stateLabel");
//			
//			stateAF = new AdapterField(stateLabel);
//			stateAF.setId("RegistrationCardDialog_stateAF");
//			stateAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.state());
//			right2.add(stateAF);
			
			stateListStore = new ListStore<StateViewModelData>();
			stateCombo = new EasyClearCombobox<StateViewModelData>();
			stateCombo.setId("RegistrationCardDialog_stateCombo");
			stateCombo.setStore(stateListStore);
			stateCombo.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.state());
			stateCombo.setDisplayField(CServiceConstants.STATE);
			stateCombo.setTriggerAction(TriggerAction.ALL);
			stateCombo.setForceSelection(true);
			stateCombo.setEditable(false);
			right2.add(stateCombo);
			
//			zipLabel = new Label();
//			zipLabel.setId("RegistrationCardDialog_zipLabel");
//			
//			zipAF = new AdapterField(zipLabel);
//			zipAF.setId("RegistrationCardDialog_zipAF");
//			zipAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.zip());
//			right2.add(zipAF);
			
			zipField = new TextField<String>();
			zipField.setId("RegistrationCardDialog_zipField");
			zipField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.zip());
			zipField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = zipField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 50){
						zipField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(CServiceConstants.CSERVICE_CONSTANTS.zip(), 50));
						return;
					}else{
						zipField.clearInvalid();
						return;
					}
				}
			});
			right2.add(zipField);
			
			lc2.add(left2, new ColumnData(.55));
			lc2.add(right2, new ColumnData(.45));
			add(lc2);
			
			lc3 = new LayoutContainer();
			lc3.setId("RegistrationCardDialog_lc3");
		    lc3.setLayout(new ColumnLayout());
		    lc3.setBorders(true);
			
		    LayoutContainer left3 = new LayoutContainer();
		    left3.setId("RegistrationCardDialog_left3");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(180);
			left3.setLayout(layout);
			
			specialServicesLabel = new Label();
			specialServicesLabel.setId("RegistrationCardDialog_specialServicesLabel");
			
			specialServicesAF = new AdapterField(specialServicesLabel);
			specialServicesAF.setId("RegistrationCardDialog_specialServicesAF");
			specialServicesAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.specialServices());
			left3.add(specialServicesAF);
			
//			remarksLabel = new Label();
//			remarksLabel.setId("RegistrationCardDialog_remarksLabel");
//			
//			remarksAF = new AdapterField(remarksLabel);
//			remarksAF.setId("RegistrationCardDialog_remarksAF");
//			remarksAF.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks());
//			left3.add(remarksAF);
			
			remarksField = new TextField<String>();
			remarksField.setId("RegistrationCardDialog_remarksField");
			remarksField.setFieldLabel(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks());
			remarksField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = remarksField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 1024){
						remarksField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks(), 1024));
						return;
					}else{
						remarksField.clearInvalid();
						return;
					}
				}
			});
			left3.add(remarksField);
			
			billingInstructionLabel = new Label();
			billingInstructionLabel.setId("RegistrationCardDialog_billingInstructionLabel");
			
			billingInstructionAF = new AdapterField(billingInstructionLabel);
			billingInstructionAF.setId("RegistrationCardDialog_billingInstructionAF");
			billingInstructionAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.billingInstruction());
			left3.add(billingInstructionAF);
			
			guaranteeLabel = new Label();
			guaranteeLabel.setId("RegistrationCardDialog_guaranteeLabel");
			
			guaranteeAF = new AdapterField(guaranteeLabel);
			guaranteeAF.setId("RegistrationCardDialog_guaranteeAF");
			guaranteeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.guarantee());
			left3.add(guaranteeAF);
			
//			expressCheckOutLabel = new Label();
//			expressCheckOutLabel.setId("RegistrationCardDialog_expressCheckOutLabel");
//			
//			expressCheckOutAF = new AdapterField(expressCheckOutLabel);
//			expressCheckOutAF.setId("RegistrationCardDialog_expressCheckOutAF");
//			expressCheckOutAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.expressCheckOut());
//			left3.add(expressCheckOutAF);
			
			expressCheckOutListStore = new ListStore<YesNoGWTEnumModelData>();
			for(YesNoGWTEnum yn : YesNoGWTEnum.values()){
				expressCheckOutListStore.add(new YesNoGWTEnumModelData(yn));
			}
			expressCheckOutCombo = new EasyClearCombobox<YesNoGWTEnumModelData>();
			expressCheckOutCombo.setId("RegistrationCardDialog_expressCheckOutCombo");
			expressCheckOutCombo.setStore(expressCheckOutListStore);
			expressCheckOutCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.expressCheckOut());
			expressCheckOutCombo.setDisplayField(CoreConstants.FULLTNAME);
			expressCheckOutCombo.setTriggerAction(TriggerAction.ALL);
			expressCheckOutCombo.setForceSelection(true);
			expressCheckOutCombo.setEditable(false);
			left3.add(expressCheckOutCombo);
			
			ta = new TextArea();
			ta.setId("RegistrationCardDialog_ta");
			ta.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.termsAndConditions());
			ta.setReadOnly(true);
			ta.setBorders(true);
			left3.add(ta);
			
			LayoutContainer right3 = new LayoutContainer();
			right3.setId("RegistrationCardDialog_right3");
			layout = new FormLayout();
			layout.setLabelAlign(LabelAlign.LEFT);
			layout.setLabelWidth(120);
			layout.setDefaultWidth(160);
			right3.setLayout(layout);
			
//			paymentCommentLabel = new Label();
//			paymentCommentLabel.setId("RegistrationCardDialog_paymentCommentLabel");
//			
//			paymentCommentAF = new AdapterField(paymentCommentLabel);
//			paymentCommentAF.setId("RegistrationCardDialog_paymentCommentAF");
//			paymentCommentAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentComment());
//			right3.add(paymentCommentAF);
			
			paymentCommentField = new TextField<String>();
			paymentCommentField.setId("RegistrationCardDialog_paymentCommentField");
			paymentCommentField.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentComment());
			paymentCommentField.addListener(Events.OnBlur,new Listener<FieldEvent>() {
				@Override
				public void handleEvent(FieldEvent be) {
					String value = paymentCommentField.getValue();
					if(value != null && !value.trim().isEmpty() && value.trim().length() > 500){
						paymentCommentField.forceInvalid(ShareGWTConstants.SHARE_GWT_MESSAGES.outOfLength(ShareGWTConstants.SHARE_GWT_CONSTANTS.remarks(), 500));
						return;
					}else{
						paymentCommentField.clearInvalid();
						return;
					}
				}
			});
			right3.add(paymentCommentField);
			
//			paymentMethodLabel = new Label();
//			paymentMethodLabel.setId("RegistrationCardDialog_paymentMethodLabel");
//			
//			paymentMethodAF = new AdapterField(paymentMethodLabel);
//			paymentMethodAF.setId("RegistrationCardDialog_paymentMethodAF");
//			paymentMethodAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod());
//			right3.add(paymentMethodAF);
			
			paymentMethodListStore = new ListStore<PaymentMethodModelData>();
			paymentMethodCombo = new EasyClearCombobox<PaymentMethodModelData>();
			paymentMethodCombo.setId("RegistrationCardDialog_paymentMethodCombo");
			paymentMethodCombo.setStore(paymentMethodListStore);
			paymentMethodCombo.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.paymentMethod());
			paymentMethodCombo.setDisplayField(CServiceConstants.PAY_MENT_METHOD);
			paymentMethodCombo.setTriggerAction(TriggerAction.ALL);
			paymentMethodCombo.setForceSelection(true);
			paymentMethodCombo.setEditable(false);
			right3.add(paymentMethodCombo);
			
			dbCodeLabel = new Label();
			dbCodeLabel.setId("RegistrationCardDialog_dbCodeLabel");
			
			dbCodeAF = new AdapterField(dbCodeLabel);
			dbCodeAF.setId("RegistrationCardDialog_dbCodeAF");
			dbCodeAF.setFieldLabel(CServiceConstants.CSERVICE_CONSTANTS.dbCode());
			right3.add(dbCodeAF);
			
			lc3.add(left3, new ColumnData(.55));
			lc3.add(right3, new ColumnData(.45));
			add(lc3);
			
			exportBtn = new FormButton(CoreConstants.CORE_CONSTANTS.export());
			exportBtn.setId("RegistrationCardDialog_exportBtn_id");
			exportBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				ExportTypeDialog etd;

				@Override
				public void componentSelected(ButtonEvent ce) {
					etd = new ExportTypeDialog(new FormSubmitListener<ShareGWTEnums.ExportType[]>() {
						@Override
						public void formSubmitCallBack(FcsFormPanel<ExportType[]> form, SubmitType submitType) {
							ExportType[] array = new ExportType[1];
							form.populateEntity(array);
							genReport(array[0]);
						}

						private void genReport(ExportType type) {
							HashMap<String, String> params = new HashMap<String, String>();

							rcId = rvModel.getEntity().getRcs().get(0).getRcId();

							params.put(ReportConstants.SORT_DESC, ReportConstants.TRUE);
							params.put(CServiceReportConstants.RCID, Long.toString(rcId));
							params.put("time", String.valueOf(new Date().getTime()));

							params.put(CServiceReportConstants.REPORT_LOCALE, LocaleInfo.getCurrentLocale()
									.getLocaleName());

							reportFrame.setUrl(HttpHelper
									.genHttpRequest("report/registrationCard.do", params));
							etd.hide();
						}
					});
					etd.show();
				}
			});
			getButtonBar().add(exportBtn);
			reportFrame = new Frame();
			reportFrame.setPixelSize(1, 1);
			reportFrame.setVisible(false);
			add(reportFrame);
			
			saveButton = new FormButton(CoreConstants.CORE_CONSTANTS.save());
			saveButton.setId("RegistrationCardDialog_saveButton_id");
			saveButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					Window.alert("save regis card");
					isValid(new ValidationListener() {

						@Override
						public void onValidate(boolean good) {
							if(good){
								if(isProfile){
									submit(SubmitType.DEFAULT);
									saveButton.removeStyleName(CoreConstants.CSS_FORM_BUTTON_FOCUS);
								}else{
									populateEntity(currentRC);
									ReservationServiceController.getINSTANCE().updateRC(
										currentRC, new AsyncCallbackListener<RCView>(){
											@Override
											public void onLogin(String username) {
												ReservationServiceController.getINSTANCE().updateRC(
														currentRC, this);								
											}
											@Override
											public void onFailure(Throwable caught) {
												MessageBox.alert(
														CoreConstants.CORE_MESSAGES.operation(
															CoreConstants.CORE_CONSTANTS.system(), 
															ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
														CoreConstants.CORE_CONSTANTS.systemError(), 
														null);
											}
											@Override
											public void onSuccess(RCView result) {
												if(result != null){
													ReservationViewModelData rvmd = new ReservationViewModelData(rv);
													ReservationPanel.grid.replaceModelData(rvModel, rvmd);
													rvModel = rvmd;
													
													RegistrationCardDialog.this.hide();
													CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.edited());
												}
											}
										});
								}
							}
						}
					});
				}
			});
			getButtonBar().add(saveButton);
			
			cancelButton = new FormButton(CoreConstants.CORE_CONSTANTS.cancel());
			cancelButton.setId("RegistrationCardDialog_cancelButton_id");
			cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>(){
				@Override
				public void componentSelected(ButtonEvent ce) {
					RegistrationCardDialog.this.hide();
				}
			});
			getButtonBar().add(cancelButton);
		}

		@Override
		public void updateForm(final RCView entity) {
			guestNameField.clear();
			emailField.clear();
			mobilField.clear();
			
			categories = new ArrayList<EditableCategoryEnum>();
			for(EditableCategoryEnum ca: EditableCategoryEnum.values()){
				categories.add(ca);
			}
			
			Scheduler.get().scheduleFinally(new ScheduledCommand() {
				@Override
				public void execute() {
			        CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
				        SessionContext.getInstance().getPropertyId(), 
				        categories, 
				        new AsyncCallbackListener<EditabilityConf>(){
					        @Override
					        public void onLogin(String username) {
						        CServiceCommonServiceController.getINSTANCE().getEditabilityConf(
								    SessionContext.getInstance().getPropertyId(), 
								    categories, this);
					        }
					        @Override
					        public void onFailure(Throwable caught) {
						        MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.system(), ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
								    CoreConstants.CORE_CONSTANTS.systemError(), null);
					        }
					        @Override
					        public void onSuccess(EditabilityConf result) {
					        	if(result != null){
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.FIRST_NAME) 
					        	        || result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.LAST_NAME)){
					        	    	guestNameField.setEnabled(true);
					        	    }else{
					        	    	guestNameField.setEnabled(false);
					        	    }
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.TITLE)){
					        	    	titleCombo.setEnabled(true);
						        	}else{
						        	    titleCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.GENDER)){
					        	    	genderCombo.setEnabled(true);
						        	}else{
						        		genderCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.NATIONALITY)){
					        	    	nationalityCombo.setEnabled(true);
						        	}else{
						        		nationalityCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.NO_OF_GUEST)){
					        	    	noOfGuestCombo.setEnabled(true);
						        	}else{
						        		noOfGuestCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.SHARE_RESERVATION)){
					        	    	shareReservationCombo.setEnabled(true);
						        	}else{
						        		shareReservationCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.ARRIVAL_DATE)){
					        	    	arrivalDateField.setEnabled(true);
						        	}else{
						        		arrivalDateField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.DEPART_DATE)){
					        	    	departureDateField.setEnabled(true);
						        	}else{
						        		departureDateField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.ARRIVAL_TIME)){
					        	    	arrivalTimeField.setEnabled(true);
						        	}else{
						        		arrivalTimeField.setEnabled(false);
						        	}
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.EXPRESS_CHECK_OUT)){
					        	    	expressCheckOutCombo.setEnabled(true);
						        	}else{
						        		expressCheckOutCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.EMAIL)){
					        	    	emailField.setEnabled(true);
						        	}else{
						        		emailField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PERSONAL, EditableFieldEnum.MOBILE)){
					        	    	mobilField.setEnabled(true);
						        	}else{
						        		mobilField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.ADDRESS)){
					        	    	addressField.setEnabled(true);
						        	}else{
						        		addressField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.CITY)){
					        	    	cityField.setEnabled(true);
						        	}else{
						        		cityField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.STATE)){
					        	    	stateCombo.setEnabled(true);
						        	}else{
						        		stateCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.COUNTRY)){
					        	    	countryCombo.setEnabled(true);
					        	    	stateCombo.setEnabled(true);
						        	}else{
						        		countryCombo.setEnabled(false);
						        		stateCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.POSTAL)){
					        	    	zipField.setEnabled(true);
						        	}else{
						        		zipField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.CONTACT, EditableFieldEnum.PHONE)){
					        	    	phoneField.setEnabled(true);
						        	}else{
						        		phoneField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PAYMENT, EditableFieldEnum.PAYMENT_COMMENT)){
					        	    	paymentCommentField.setEnabled(true);
						        	}else{
						        		paymentCommentField.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.PAYMENT, EditableFieldEnum.PAYMENT_METHOD)){
					        	    	paymentMethodCombo.setEnabled(true);
						        	}else{
						        		paymentMethodCombo.setEnabled(false);
						        	}
					        	    
					        	    if(result.isEditable(EditableCategoryEnum.RESERVATION, EditableFieldEnum.RC_REMARKS)){
					        	    	remarksField.setEnabled(true);
						        	}else{
						        		remarksField.setEnabled(false);
						        	}
					        	}
					        }
				    });
				}
			});
			
			countryListStore.removeAll();
			for(CountryView cv : countryList){
				countryListStore.add(new CountryViewModelData(cv));
			}
			currentRC = entity;
			confirmationNoLabel.setText(rv.getConfirmationNo());
			if (rv.getMemberships() != null && rv.getMemberships().size() > 0) {
				spgNoLabel.setText(rv.getMemberships().get(0).getNumber());
			}else{
				spgNoLabel.setText("");
			}
			//			arrivalDateTimeLabel.setText(CServiceConstants.DATE_FORMAT3.format(rv.getArrivalDateTime()));
			arrivalDateField.setValue(CServiceConstants.DATE_FORMAT1.parse(
					CServiceConstants.DATE_FORMAT1.format(entity.getArrivalDateTime())));
			arrivalTimeField.setValue(entity.getArrivalDateTime());
			
//			departualDateLabel.setText(CServiceConstants.DATE_FORMAT1.format(rv.getDepartureDateTime()));
			departureDateField.setValue(CServiceConstants.DATE_FORMAT1.parse(
					CServiceConstants.DATE_FORMAT1.format(entity.getDepartureDateTime())));
			
			gsaLabel.setText(SessionContext.getInstance().getUsername());
//			gsaField.setValue(value);
			
			StringBuilder sb = null;
			if (entity.getRates() != null && entity.getRates().size() > 0) {
				if(entity.getRates().size() > 1){
					roomRateValueLabel.setText("<html><U>" + CServiceConstants.CSERVICE_CONSTANTS.seeDetail() + "</U></html>");
					roomRateValueLabel.sinkEvents(Event.ONCLICK);
					roomRateValueLabel.addListener(Events.OnClick, new Listener<BaseEvent>(){
						@Override
						public void handleEvent(BaseEvent be) {
							rateDetailDialog = RoomRateDetailDialog.getInstance(currentRC.getRates());
							rateDetailDialog.getFormPanel().updateForm(rv);
							rateDetailDialog.show();
						}
					});
				}else{
					roomRateValueLabel.removeAllListeners();
					sb = new StringBuilder();
					sb.append(CServiceConstants.DATE_FORMAT1.format(entity.getRates().get(0).getFromDate()));
					sb.append(CServiceConstants.CSERVICE_CONSTANTS.to());
					sb.append(CServiceConstants.DATE_FORMAT1.format(entity.getRates().get(0).getToDate()));
					sb.append(" " + entity.getRates().get(0).getRate());
					roomRateValueLabel.setText(sb == null ? null : sb.toString());
				}
			}
			
//			guestNameLabel.setText(rv.getGuestFirstName() + "," +rv.getGuestLastName());
			guestNameField.setValue(entity.getGuestProfile().getFirstName() + "," + entity.getGuestProfile().getLastName());			
			
//			titleLabel.setText(entity.getGuestProfile().getTitleId());
			titleListStore.removeAll();
			for(Title title : reservationConf.getTitleList()){
				titleListStore.add(new TitleModelData(title));
			}                  
			
			titleCombo.setValue(titleListStore.findModel(CoreConstants.ID, entity.getGuestProfile().getTitleId()));
			
//			genderLabel.setText(entity.getGuestProfile().getGenderMale() ? 
//					GenderGWTEnum.MALE.getDisplayStr() : GenderGWTEnum.FEMALE.getDisplayStr());
			
			genderCombo.setValue(entity.getGuestProfile().getGenderMale() 
					? genderListStore.findModel(CoreConstants.ID, GenderEnum.MALE.toString())
					: genderListStore.findModel(CoreConstants.ID, GenderEnum.FEMALE.toString()));
			
			noOfGuestCombo.setValue(noOfGuestStore.findModel(CoreConstants.FULLTNAME, rv.getNumberOfGuest()));
			roomSuiteLabel.setText(rv.getRoomNumber());
			roomTypeLabel.setText(rv.getRoomType());
//			shareReservationLabel.setText(entity.isShareReservation() 
//					? CoreConstants.CORE_CONSTANTS.yes() 
//					: CoreConstants.CORE_CONSTANTS.no());
			shareReservationCombo.setValue(entity.isShareReservation() 
					              ? shareReservationListStore.findModel(CoreConstants.ID, YesNoEnum.YES.toString())
					              : shareReservationListStore.findModel(CoreConstants.ID, YesNoEnum.NO.toString()));
			
			depositLabel.setText(entity.getDeposite() + "");
//			guestInitialLabel.setText("Tl");
//			nationalityLabel.setText(entity.getGuestProfile().getNationalityId());
			nationalityListStore.removeAll();
			for(Nationality nat: reservationConf.getNationalityList()){
				nationalityListStore.add(new NationalityModelData(nat));
			}
			nationalityCombo.setValue(nationalityListStore.findModel(CoreConstants.ID, entity.getGuestProfile().getNationalityId()));
			
//			eMailAddressLabel.setText(entity.getGuestProfile().getEmail());
			emailField.setValue(entity.getGuestProfile().getEmail());
			
//			mobileLabel.setText(entity.getGuestProfile().getMobile());
			mobilField.setValue(entity.getGuestProfile().getMobile());
			
//			homeRadio.setValue(value);
//		    businessRadio.setValue(value);
//			addressLabel.setText("Rm 1504, 15/F, city plaza 4, Taikoo Shing");
//			cityLabel.setText("Shen Zhen");
//			countryLabel.setText("China");
//			phoneLabel.setText("+86 1581 4775 509");
//			stateLabel.setText("NA");
//			zipLabel.setText("");
			
			if("HOME".equals(entity.getGuestProfile().getContactInfo().getType())){
				homeRadio.setValue(true);
				businessRadio.setValue(false);
			}else{
				homeRadio.setValue(false);
				businessRadio.setValue(true);
			}
			addressField.setValue(entity.getGuestProfile().getContactInfo().getAddress());
			cityField.setValue(entity.getGuestProfile().getContactInfo().getCity());
			countryCombo.setValue(countryListStore.findModel(
				CoreConstants.ID, 
				entity.getGuestProfile().getContactInfo().getCountryId()));
//			phoneLabel.setText(entity.getGuestProfile().getContactInfo().getPhone1());
			phoneField.setValue(entity.getGuestProfile().getContactInfo().getPhone1());
			Timer timer = new Timer(){
				@Override
				public void run() {
					if(loadStateOver){
						stateCombo.setValue(stateListStore.findModel(
								CoreConstants.ID, 
								entity.getGuestProfile().getContactInfo().getStateId()));
						cancel();
					}
				}
			};
			timer.scheduleRepeating(100);
			
			zipField.setValue(entity.getGuestProfile().getContactInfo().getZipPostal());
			
			specialServicesLabel.setText(entity.getSpecialService());
//			remarksLabel.setText(entity.getRcRemarks());
			remarksField.setValue(entity.getRcRemarks());
			billingInstructionLabel.setText(entity.getBillingInstruction());
			guaranteeLabel.setText(entity.getGuarantee());
//			expressCheckOutLabel.setText(entity.getExpressCheckout()
//					? CoreConstants.CORE_CONSTANTS.yes() 
//					: CoreConstants.CORE_CONSTANTS.no());
			expressCheckOutCombo.setValue(entity.getExpressCheckout() 
		              ? expressCheckOutListStore.findModel(CoreConstants.ID, YesNoEnum.YES.toString())
		              : expressCheckOutListStore.findModel(CoreConstants.ID, YesNoEnum.NO.toString()));
			
			ta.setValue(reservationConf.getTandc());
			
//			paymentCommentLabel.setText(entity.getPaymentComment());
			paymentCommentField.setValue(entity.getPaymentComment());
//			paymentMethodLabel.setText(entity.getPaymentMethodId());	
			
			paymentMethodListStore.removeAll();
			for(PaymentMethod pm : reservationConf.getPaymentMethodList()){
				paymentMethodListStore.add(new PaymentMethodModelData(pm));
			}
			paymentMethodCombo.setValue(paymentMethodListStore.findModel(CoreConstants.ID, entity.getPaymentMethodId()));
			
			dbCodeLabel.setText(entity.getDirectBillCode());
		}

		@SuppressWarnings("deprecation")
		@Override
		public void populateEntity(RCView entity) {
			Date date = arrivalDateField.getValue();
			Date time = arrivalTimeField.getDateTimeValue();
			date.setHours(time.getHours());
			date.setMinutes(time.getMinutes());
			entity.setArrivalDateTime(date);
			rv.setArrivalDateTime(date);
			
			Date depDate = departureDateField.getValue();
			depDate.setHours(rv.getDepartureDateTime().getHours());
			depDate.setMinutes(rv.getDepartureDateTime().getMinutes());
			rv.setDepartureDateTime(depDate);
			entity.setDepartureDateTime(depDate);
			
			String strs = guestNameField.getValue();
			entity.getGuestProfile().setFirstName(strs.split(",")[0].trim());
			entity.getGuestProfile().setLastName(strs.split(",")[1].trim());
			rv.setGuestFirstName(strs.split(",")[0].trim());
			rv.setGuestLastName(strs.split(",")[1].trim());
			
			if (titleCombo.getValue() != null) {
				entity.getGuestProfile().setTitleId(titleCombo.getValue().getEntity().getTitleId());
			}
			if (genderCombo.getValue() != null) {
				entity.getGuestProfile().setGenderMale(
						genderCombo.getValue().getEnum().equals(GenderGWTEnum.MALE));
			}
			if (noOfGuestCombo.getValue() != null) {
				entity.setNumberOfGuest(noOfGuestCombo.getValue().getNum().intValue());
				rv.setNumberOfGuest(noOfGuestCombo.getValue().getNum().intValue());
			}else{
				entity.setNumberOfGuest(0);
				rv.setNumberOfGuest(0);
			}
			
			if (shareReservationCombo.getValue() != null) {
				entity.setShareReservation(shareReservationCombo.getValue()
					.getEnum().equals(YesNoGWTEnum.YES));
			}
			if (nationalityCombo.getValue() != null) {
				entity.getGuestProfile().setNationalityId(
						nationalityCombo.getValue().getEntity().getNationalityId());
			}
			entity.getGuestProfile().setEmail(emailField.getValue());
			entity.getGuestProfile().setMobile(mobilField.getValue());
			
			entity.getGuestProfile().getContactInfo().setType(homeRadio.getValue() ? "HOME" : "BUSINESS");
			entity.getGuestProfile().getContactInfo().setAddress(addressField.getValue());
			entity.getGuestProfile().getContactInfo().setCity(cityField.getValue());
			if (countryCombo.getValue() != null) {
				entity.getGuestProfile().getContactInfo().setCountryId(
					countryCombo.getValue().getEntity().getCountryId());
			}
			entity.getGuestProfile().getContactInfo().setPhone1(phoneField.getValue());
			
			entity.setRcRemarks(remarksField.getValue());
			if (expressCheckOutCombo.getValue() != null) {
				entity.setExpressCheckout(expressCheckOutCombo.getValue()
					.getEnum().equals(YesNoGWTEnum.YES));
			}
			entity.setPaymentComment(paymentCommentField.getValue());
			if (paymentMethodCombo.getValue() != null) {
				entity.setPaymentMethodId(paymentMethodCombo.getValue()
					.getEntity().getMethodId());
			}
		}

		@Override
		public void setFocus() {
			// TODO Auto-generated method stub
			
		}
	}
	@Override
	public void formSubmitCallBack(FcsFormPanel<RCView> form,
			SubmitType submitType) {
		if (listener != null) {
			listener.formSubmitCallBack(form, submitType);
		}
	}

	@Override
	protected FcsFormPanel<RCView> getFormPanel() {
		return formPanel;
	}

	@Override
	public void hide() {
		super.hide();
		reportFrame.setUrl("");
	}
	
	private boolean isValidTime(String time){
		boolean isValid = true;
		if(!time.contains(":")){
			isValid = false;
		}else{
			String[] ts= time.split(":");
			if(ts.length != 2){
				isValid = false;
			}else{
				if((ts[0].trim().length() != 2) 
				   || (ts[1].trim().length() != 2)){
					isValid = false;
				}else{
					if((!Character.isDigit(ts[0].trim().charAt(0))) 
						|| (!Character.isDigit(ts[0].trim().charAt(1)))
						|| (!Character.isDigit(ts[1].trim().charAt(0))) 
					    || (!Character.isDigit(ts[1].trim().charAt(1)))){
						isValid = false;
					}else if((Integer.parseInt(ts[0].trim()) > 24)
							 || (Integer.parseInt(ts[1].trim()) > 59)){
						isValid = false;
					}
				}
			}
		}
		return isValid;
	}
	
	private boolean isValidName(String name){
		boolean isValid = true;
		String[] ns = name.split(",");
		String firstName = ns[0].trim();
		String lastName = ns[1].trim();
		if(firstName.length()>255 || lastName.length()>255){
			isValid = false;
		}
		return isValid;
	}
	
	@SuppressWarnings("deprecation")
	private boolean isValidMobile(String mobile){
		boolean isValid = true;
		for(int i = 0; i< mobile.substring(1).length(); i++){
			if(!Character.isDigit(mobile.substring(1).charAt(i))
					   && !Character.isSpace(mobile.substring(1).charAt(i))){
				isValid = false;
				break;
			}
		}
		return isValid;
	}
}
